package work.MrSun.xyq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import work.MrSun.xyq.model.XyqMessage;
import work.MrSun.xyq.service.XyqMessageDetailService;
import work.MrSun.xyq.service.XyqMessageImagesService;
import work.MrSun.xyq.service.XyqUserService;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author MrSun
 * @Date 2023/4/12 19:33
 */
@RestController
public class SearchMessageController {
    @Autowired
    private XyqMessageDetailService xyqMessageDetailService;
    @Autowired
    private XyqUserService xyqUserService;
    @Autowired
    private XyqMessageImagesService xyqMessageImagesService;

    /*
     *接收请求中传递的分类 ID 和关键词。
     */
    @PostMapping("/search/{categoryId}/{keyword}")
    public List<XyqMessage> getMessageByCategoryAndKeyword(@PathVariable Integer categoryId,
                                                           @PathVariable String keyword,
                                                           @RequestParam(defaultValue = "1") Integer pageNum,
                                                           @RequestParam(defaultValue = "5") Integer pageSize) {
        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        //调用方法获取符合条件的信息
        List<XyqMessage> allMessage = xyqMessageDetailService.getMessageByCategoryAndKeyword(categoryId, keyword);
        //将信息封装
        PageInfo<XyqMessage> pageInfo = new PageInfo<>(allMessage);
        List<XyqMessage> list = pageInfo.getList();
        return new MessageDetailController().getImage(list, xyqUserService, xyqMessageImagesService);
    }
//    getMessageByCategoryId
    /**
     * 接收请求中的关键词还有页码
     *
     * @param keyword
     * @param pageNumber
     * @return
     */
    @PostMapping("/searchByKeyword/{keyword}/{pageNumber}")
    public List<XyqMessage> getMessageByCategoryAndKeyword(@PathVariable String keyword,
                                                           @PathVariable Integer pageNumber) {
        //开始分页  当前的页码和每页显示多少条记录
        PageHelper.startPage(pageNumber, 3);
        List<XyqMessage> messageByKeyword = xyqMessageDetailService.getMessageByKeyword(keyword);
        //进行封装
        PageInfo<XyqMessage> pageInfo = new PageInfo<>(messageByKeyword);
        //判断是不是最后一页
        if (pageInfo.getPageNum() < pageNumber) {
            List list1 = new LinkedList();
            list1.add(200);
            return list1;
        }
        //从page
        List<XyqMessage> allMessage = pageInfo.getList();
        return new MessageDetailController().getImage(allMessage, xyqUserService, xyqMessageImagesService);
    }
}
